package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.common.utils.PageUtil;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.bo.model.*;
import com.geniusk.system.model.pojo.Model;
import com.geniusk.system.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型管理接口
 */
@RestController
@RequestMapping("/system/model")
public class ModelController {

    @Autowired
    private ModelService modelService;

    /**
     * ID查询模型
     */
    @PostMapping("/selectModelById")
    @PreFuncAuth("SYS:MODEL:SELECT'")
    public JSONResult selectModelById(@Valid @RequestBody SelectModelByIdBO selectModelByIdBO) {
        Model model = modelService.selectModelById(selectModelByIdBO.getModelId());
        Map<String, Object> result = new HashMap<>();
        result.put("model",model);
        return JSONResult.ok(result);
    }

    /**
     * ID查询物模型详情
     */
    @PostMapping("/selectModelInfoById")
    public JSONResult selectModelInfoById(@Valid @RequestBody SelectModelByIdBO selectModelByIdBO) {
        Model model = modelService.selectModelById(selectModelByIdBO.getModelId());
        Long modelId = model.getModelId();
        Map<String, Object> result = new HashMap<>();
        result.put("model",modelService.selectModelVOByModelId(modelId));
        return JSONResult.ok(result);
    }

    /**
     * 分页查询模型
     */
    @PostMapping("/selectModelByPage")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelByPage(@Valid @RequestBody SelectModelByPageBO selectModelByPageBO) {
        int pageNum = selectModelByPageBO.getPageNum();
        int pageSize = selectModelByPageBO.getPageSize();
        Model model = JSONUtil.parse(selectModelByPageBO).toBean(Model.class);
        model.setCreateBy(SecurityUtil.getUsername());
        PageUtil.startPage(pageNum,pageSize);
        List<Model> modelList = modelService.selectModelByPage(model);
        Map<String,Object> result = PageUtil.getResultTableData(modelList);
        return JSONResult.ok(result);
    }

    /**
     * 查询全部模型
     */
    @PostMapping("/selectAllModel")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectAllModel() {
        List<Model> modelList = modelService.selectAllModel();
        Map<String,Object> result = new HashMap<>();
        result.put("list",modelList);
        return JSONResult.ok(result);
    }

    /**
     * 添加模型
     */
    @PostMapping("/insertModel")
    @PreFuncAuth("SYS:MODEL:INSERT")
    public JSONResult insertModel(@Valid @RequestBody InsertModelBO insertModelBO) {
        Model model = JSONUtil.parse(insertModelBO).toBean(Model.class);
        Long modelId = IdUtil.getSnowflake().nextId();
        model.setModelId(modelId);
        model.setCreateBy(SecurityUtil.getUsername());
        int rows = modelService.insertModel(model);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改模型
     */
    @PostMapping("/updateModel")
    @PreFuncAuth("SYS:MODEL:UPDATE")
    public JSONResult updateModel(@Valid @RequestBody UpdateModelBO updateModelBO) {
        Model model = JSONUtil.parse(updateModelBO).toBean(Model.class);
        model.setUpdateBy(SecurityUtil.getUsername());
        int rows = modelService.updateModel(model);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除模型
     */
    @PostMapping("/deleteModelByIds")
    @PreFuncAuth("SYS:MODEL:DELETE")
    public JSONResult deleteModelByIds(@Valid @RequestBody DeleteModelByIdsBO deleteModelByIdsBO) {
        int rows = modelService.deleteModelByIds(deleteModelByIdsBO.getModelIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
